package test.ground.kafka;

import com.wang.tool.monitor.InvokeTemplate;
import com.wang.tool.monitor.template.AbsMonitorInvoker;
import com.wang.tool.trace.TraceHolder;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Aspect  
@Component
@Order(0)
public class KafkaConsumerTraceAspect {

    @Around("@annotation(kafkaListener)")
    public Object aroundKafkaListener(ProceedingJoinPoint joinPoint, KafkaListener kafkaListener) throws Throwable {
        try {
            return InvokeTemplate.monitor("callback_mq", kafkaListener.groupId(), StringUtils.join(kafkaListener.topics(),","), new AbsMonitorInvoker<Object>() {
                @Override
                public Object internalCall() throws Throwable {
                    return joinPoint.proceed();
                }
            });
        } finally {
            TraceHolder.clear();
        }
    }  
}